package three;

import java.util.Arrays;
import java.util.Scanner;

public class test8597 {
    private static int[] a;
    private static int[][] flag;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        a = new int[n + 1];
        flag = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);
        fun(n, m);
    }

    private static void fun(int n, int m) {
        for (int i = 1; i <= n; i++) {
            flag[i][1] = (int) Math.pow((a[i] - a[1]), 2);
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 2; j <= m; j++) {
                int min = 1000000000;
                for (int k = 1; k <= i - j + 1; k++) {
                    if (min > flag[i - k][j - 1] + ((int) Math.pow((a[i] - a[i - k + 1]), 2))) {
                        min = flag[i - k][j - 1] + ((int) Math.pow((a[i] - a[i - k + 1]), 2));
                    }
                }
                flag[i][j] = min;
            }
        }
        System.out.println(flag[n][m]);
    }
}
